﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Unijovem.Domain;

namespace Unijovem.UI.Controllers
{ 
    public class Autor2Controller : Controller
    {
        private DatabaseEntities db = new DatabaseEntities();

        //
        // GET: /Autor2/

        public ViewResult Index()
        {
            var autors = db.Autors.Include("Arquivo");
            return View(autors.ToList());
        }

        //
        // GET: /Autor2/Details/5

        public ViewResult Details(int id)
        {
            Autor autor = db.Autors.Single(a => a.Id == id);
            return View(autor);
        }

        //
        // GET: /Autor2/Create

        public ActionResult Create()
        {
            ViewBag.IdArquivo = new SelectList(db.Arquivoes, "Id", "Nome");
            return View();
        } 

        //
        // POST: /Autor2/Create

        [HttpPost]
        public ActionResult Create(Autor autor)
        {
            if (ModelState.IsValid)
            {
                db.Autors.AddObject(autor);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.IdArquivo = new SelectList(db.Arquivoes, "Id", "Nome", autor.IdArquivo);
            return View(autor);
        }
        
        //
        // GET: /Autor2/Edit/5
 
        public ActionResult Edit(int id)
        {
            Autor autor = db.Autors.Single(a => a.Id == id);
            ViewBag.IdArquivo = new SelectList(db.Arquivoes, "Id", "Nome", autor.IdArquivo);
            return View(autor);
        }

        //
        // POST: /Autor2/Edit/5

        [HttpPost]
        public ActionResult Edit(Autor autor)
        {
            if (ModelState.IsValid)
            {
                db.Autors.Attach(autor);
                db.ObjectStateManager.ChangeObjectState(autor, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdArquivo = new SelectList(db.Arquivoes, "Id", "Nome", autor.IdArquivo);
            return View(autor);
        }

        //
        // GET: /Autor2/Delete/5
 
        public ActionResult Delete(int id)
        {
            Autor autor = db.Autors.Single(a => a.Id == id);
            return View(autor);
        }

        //
        // POST: /Autor2/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Autor autor = db.Autors.Single(a => a.Id == id);
            db.Autors.DeleteObject(autor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}